package cn.springmix.base.lang.exception;

import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

import java.util.function.Supplier;

/**
 * @author xufeng
 * @description: 常用断言工具国际化 可从Assert类 copy
 * @date 2022-10-14 16:23
 */
public class Asserts {

    /**
     * Assert that an object is {@code null}.
     * <pre class="code">Assert.isNull(value, "The value must be null");</pre>
     *
     * @param object  the object to check
     * @param message the exception message to use if the assertion fails
     * @throws IllegalArgumentException if the object is not {@code null}
     */
    public static void isNull(@Nullable Object object, String message) {
        if (object != null) {
            Exceptions.throwEx(BaseException.class, message);
        }
    }

    /**
     * Assert that an object is {@code null}.
     * <pre class="code">
     * Assert.isNull(value, () -&gt; "The value '" + value + "' must be null");
     * </pre>
     */
    public static void isNull(@Nullable Object object, Supplier<String> messageSupplier) {
        if (object != null) {
            Exceptions.throwEx(BaseException.class, nullSafeGet(messageSupplier));
        }
    }

    /**
     * 普通判空工具
     *
     * @param object
     * @param message 国际化字符串key
     */
    public static void notNull(@Nullable Object object, String message) {
        if (object == null) {
            Exceptions.throwEx(BaseException.class, message);
        }
    }

    /**
     * @param messageSupplier 带逻辑过程函数式回调 根据调用方的逻辑返回字符串
     *                        示例： Asserts.isNull(value, () -> "The value '" + value + "' must be null");
     * @return
     */
    public static void notNull(@Nullable Object object, Supplier<String> messageSupplier) {
        if (object == null) {
            Exceptions.throwEx(BaseException.class, nullSafeGet(messageSupplier));
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            Exceptions.throwEx(BaseException.class, message);
        }
    }

    /**
     * @param messageSupplier 带逻辑过程函数式回调 根据调用方的逻辑返回字符串
     * @return
     */
    public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
        if (!expression) {
            Exceptions.throwEx(BaseException.class, nullSafeGet(messageSupplier));
        }
    }

    public static void state(boolean expression, String message) {
        if (!expression) {
            Exceptions.throwEx(BaseException.class, message);
        }
    }

    /**
     * 判断状态断言工具
     *
     * @param expression
     * @param messageSupplier
     */
    public static void state(boolean expression, Supplier<String> messageSupplier) {
        if (!expression) {
            Exceptions.throwEx(BaseException.class, nullSafeGet(messageSupplier));
        }
    }


    public static void notEmpty(@Nullable Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Assert.notEmpty(array, () -&gt; "The " + arrayType + " array must contain elements");
     *
     * @param array
     * @param messageSupplier
     */
    public static void notEmpty(@Nullable Object[] array, Supplier<String> messageSupplier) {
        if (ObjectUtils.isEmpty(array)) {
            throw new IllegalArgumentException(nullSafeGet(messageSupplier));
        }
    }

    @Nullable
    private static String nullSafeGet(@Nullable Supplier<String> messageSupplier) {
        return messageSupplier != null ? (String) messageSupplier.get() : null;
    }


}
